package com.cm.behavior.chainofresponsibilitypattern;

/**
 * 责任链模式
 * 责任链在spring中的应用
 *  1.Servlet 中的过滤器 Filter
 *  2.在 SpringMVC 中的 Interceptor 也用了责任链模式
 * @author cm.g
 * @date 2023/8/18 17:30
 */
public abstract class Handler {
    private Handler next;

    public Handler getNext() {
        return next;
    }

    public Handler setNext(Handler next) {
        this.next = next;
        return next;
    }
    public abstract void handle(Object request);

    /**
     * 责任链的调用方式，
     * 1.写死责任链的调用顺序
     * 2.给责任链设置order，由spring注入list，for循环
     * @param args .
     */
    public static void main(String[] args) {
        Handler concreteHandler1 = new ConcreteHandler1();
        Handler concreteHandler2 = new ConcreteHandler2();
        Handler concreteHandler3 = new ConcreteHandler3();

        concreteHandler1.setNext(concreteHandler2).setNext(concreteHandler3);

        concreteHandler1.handle("my request.");
    }

}
